package cn.jiyun.service.impl;

import cn.jiyun.dao.StudentDao;
import cn.jiyun.entity.PageResult;
import cn.jiyun.entity.QueryPageBean;
import cn.jiyun.pojo.Student;
import cn.jiyun.service.StudentService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Student> page = studentDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Student student, Integer[] checkitemIds) {

        //添加基本信息
        studentDao.add(student);

        //添加中间表
        for (Integer checkitemId : checkitemIds) {
            studentDao.addts(student.getId(),checkitemId);
        }
    }

    @Override
    public void edit(Student student, Integer[] checkitemIds) {

        //修改基本信息
        studentDao.updateByPrimaryKeySelective(student);

        //删除中间表
        studentDao.delBySid(student.getId());

        //添加
        for (Integer checkitemId : checkitemIds) {
            studentDao.addts(student.getId(),checkitemId);
        }
    }

    @Override
    public void delete(Integer id) {

        //删除中间表
        studentDao.delBySid(id);

        //删除基本信息
        studentDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return studentDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void doJob() {
        List<Student> list = studentDao.selectAll();
        for (Student student : list) {
            student.setAge(student.getAge()+1);
            studentDao.updateByPrimaryKeySelective(student);
        }
    }
}
